<?php defined('SYSPATH') or die('No direct script access.');
require_once(APPPATH. 'classes/model/Model_Mysql.php');

class Model_UserLogin extends Model_Mysql {
	const NETWORK_SELF = 1;
	const NETWORK_FB = 2;
	
	const ST_CREATED = 1;
	const ST_ACTIVATED = 2;
	const ST_BANNED = 10;
	public function getUid($username, $password) {
		//if not valid then return null
		$query = DB::select()->from('users_login')->where('username',"=",$username)
				->and_where('network',"=",self::NETWORK_SELF)->and_where('status',"=",self::ST_ACTIVATED);
		$result = $this->execute($query);
		if(count($result) < 1) return null;
		if($this->encryptPasswd($password) == $result[0]['password']) {
			return $result[0]['uid'];
		}
		return null;
	}
	public function getEmail($uid) {
		$query = DB::select('username')->from('users_login')->where('uid', '=', $uid);
		$result = $this->execute_single($query);
		if($result == false) return null;
		return $result['username'];
	}
	public function getPendingUid($username, $password) {
		//if not valid then return null
		$query = DB::select()->from('users_login')->where('username',"=",$username)
				->and_where('network',"=",self::NETWORK_SELF)->and_where('status',"=",self::ST_CREATED);
		$result = $this->execute($query);
		if(count($result) < 1) return null;
		if($this->encryptPasswd($password) == $result[0]['password']) {
			return $result[0]['uid'];
		}
		return -1;
	}
	public function createLogin($username, $password, $network, $status) {
		$password = $this->encryptPasswd($password);
		$query = DB::insert('users_login')->columns(array('username', 'network', 'password','create_date', 'status'))
				->values(array($username, $network, $password, time(), $status));
		list($id, $count) = $this->execute($query);
		if($count > 0) return $id;
		return -1;
	}
	public function getUidByEmail($email, $network, &$status = null)
	{
		$query = DB::select('uid', 'status')->from('users_login')->where('username',"=",$email)->and_where('network',"=",$network);
		$result = $this->execute_single($query);
		if($result == false) return 0;
		if($status != null) $status = $result['status'];
		return $result['uid'];
	}
	public function setStatus($uid, $status) {
		$query = DB::query(Database::UPDATE, "UPDATE users_login SET status=$status WHERE uid=$uid;");
		$this->execute($query);
	}
	public function setPassword($uid, $password) {
		$password = $this->encryptPasswd($password);
		$query = DB::query(Database::UPDATE, "UPDATE users_login SET password='$password' WHERE uid=$uid;");
		return $this->execute($query);
	}
	public function createFbLogin($fb, $uid)
	{
		$query = DB::insert('users_profile')->columns(array('uid', 'f_name', 'l_name', 'sex', 'pic_url', 'about_me'))
				->values(array($uid, $fb['first_name'],$fb['last_name'],1,'https://graph.facebook.com/'.$fb['id'].'/picture', ''));
		$result = $this->execute($query);
		return $result;
	}
	public function getUsers($search) {
		$query = DB::select('uid', 'username')->from('users_login')->where('username', 'like', "$search%")
		->where('network', '=', self::NETWORK_SELF)->limit(10);
		$result = $this->execute($query);
		return $result;
	}
	//reset
	public function deleteUser($uid) {
		//update email to something that is invalid email
		$prefix = time()."@";
		$query = DB::query(Database::UPDATE, "UPDATE users_login 
			SET username = CONCAT('$prefix', username) WHERE uid = $uid");
		//UPDATE query return num rows affected
		$result = $this->execute($query);
		return $result;
	}
	private function encryptPasswd($password) {
		if(Kohana::$environment === Kohana::PRODUCTION)
			return md5($password);
		else return $password;
	}
}
